//
//  BKXPictureCollectionView.swift
//  微博
//
//  Created by 田凯 on 16/4/24.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit
//设置 可重用 标识符
let BKXPictureCollectionViewCell = "BKXPictureCollectionViewCell"

class BKXPictureCollectionView: UICollectionView {
    
    // 点击加号 添加图片的闭包
    var addIamgePicture: (()->())?
    
    //图片 数组 集合
    var images: [UIImage] = [UIImage]()
    
    
    // MARK: -  向图片数组 里添加图片的 方法  供外界调用
    func addImage(image: UIImage){
//        images.append(image)
        images.insert(image, atIndex: 0)
        
        if images.count > 9{
        images.removeLast()
        
        }
        if images.count > 0{
        self.hidden = false
        
        }
        
        
        
        
        //刷新UI
        reloadData()
    
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置 视图
    func setupUI(){
    self.backgroundColor = UIColor.darkGrayColor()
        self.hidden = true
        
        dataSource = self
        delegate = self
        //注册cell 
        registerClass(BKXPictureCell.self, forCellWithReuseIdentifier:BKXPictureCollectionViewCell )
        let margin: CGFloat = 5
        let cellW = (ScreenW - margin * 2 - 20) / 3
        
        let  layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellW, height: cellW)
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
        
    
    }
    
}
// MARK: - 数据源犯法
extension BKXPictureCollectionView: UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = images.count
        if count == 0 || count == 9{
        return count
        }else{
        return count + 1
        }
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(BKXPictureCollectionViewCell, forIndexPath: indexPath) as! BKXPictureCell
        
//        cell.backgroundColor = RandomColor()
        
//        cell.image = images[indexPath.item]
        
        if indexPath.item == images.count{
        cell.image = nil
            
        }else{
            cell.image = images[indexPath.item]
        
        }
        cell.deleteBag = {[weak self] in
            self?.images.removeAtIndex(indexPath.item)
            
            
            if self?.images.count <= 0{
                self?.hidden = true
            }

            self?.reloadData()
        
        
        }
        
        
        return cell
        
        
    }
    
    //取消 cell的选中状态
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if indexPath.item == images.count{
           addIamgePicture?()
        }
        
    }
    

}

// MARK: - 自定义collectionCell
class BKXPictureCell: UICollectionViewCell{
    
    
    // 删除按钮的闭包
    var deleteBag:(()->())?
    
    var image: UIImage?{
        didSet{
//        bgImageView.image = image
            if image != nil{
            bgImageView.image = image
                bgImageView.highlightedImage = image
                deleteButton.hidden = false
                
            }else{
            bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.hidden = true
            
            }
            
            
        
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置 UI
    func setupUI(){
    
//    self.backgroundColor = RandomColor()
        //添加控件
        contentView.addSubview(bgImageView)
        
        contentView.addSubview(deleteButton)
        //添加 约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        
        }
        
        
    }
    @objc func deleteClick(){
        deleteBag?()
    
    }
    
    // MARK: - 懒加载控件
    private lazy var bgImageView : UIImageView = UIImageView()
    // MARK: - 懒加载 删除按钮
    private lazy var deleteButton: UIButton = {
    let  button = UIButton()
        //添加删除按钮的 点击事jian
        button.addTarget(self, action: "deleteClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        button.sizeToFit()
        
        return button
    
    }()

}






